import 'package:elmer_tools/common/dataBase/Connection.dart';
import 'package:elmer_tools/context/AppProvider.dart';
import 'package:elmer_tools/data/dataTable/ConfigTable.dart';
import 'package:elmer_tools/pages/home/index.dart';
import 'package:elmer_tools/pages/login/index.dart';
import 'package:elmer_tools/pages/welcome/index.dart';
import 'package:elmer_tools/service/ApiService.dart';
import 'package:elmer_tools/service/index.dart';
import 'package:flutter/material.dart';

void main() async {
    WidgetsFlutterBinding.ensureInitialized();
    // Initialize the API service
    ApiService apiService = await createApiService();
    // Set the API service to be globally accessible if needed
    // For example, you can use a provider or a global variable
    // Global.apiService = apiService; // Uncomment if you have a global variable
    runApp(MyApp(apiService: apiService));
    Connection connect = Connection(engine: "sqlite", tables: [ ConfigTable() ]);
    connect.init();
    print(connect.toString());
}

// ignore: must_be_immutable
class MyApp extends StatelessWidget {

    final ApiService apiService;

    const MyApp({super.key, required this.apiService});

    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
        apiService.setRunEnv("dev");
        return AppProvider(
            apiService: apiService,
            child: MaterialApp(
                title: 'Flutter Demo',
                theme: ThemeData(
                    primarySwatch: Colors.blue,
                ),
                home: Welcome(),
                routes: {
                    "/home": (context) => Home(),
                    "/login": (context) => Login(), // Replace with your actual user page
                },
            ),
        );
    }
}
